#include "NoiseGate.h"
#include "lang/StringFormat.hpp"
using namespace yzrilyzr_lang;
namespace yzrilyzr_dsp{
	NoiseGate::NoiseGate(){
		registerParamNormal01("Threshold", &threshold);
		registerSub("Env", &envDec);
	}
	NoiseGate::NoiseGate(u_normal_01 threshold, u_time_ms attack, u_time_ms hold, u_time_ms release,
						 int limitType) : threshold(threshold){
		envDec.attack=attack;
		envDec.hold=hold;
		envDec.release=release;
		envDec.type=limitType;
	}
	u_sample NoiseGate::procDsp(u_sample v){
		envDec.procDsp(v);
		u_sample env=envDec.getEnvValue();
		if(env < threshold){
			return 0;
		}
		return v;
	}
	void NoiseGate::init(u_sample_rate sampleRate){
		envDec.init(sampleRate);
	}
	void NoiseGate::resetMemory(){
		envDec.resetMemory();
	}
	void NoiseGate::cloneParam(DSP * obj1){
		if(auto other=dynamic_cast<NoiseGate *>(obj1)){
			threshold=other->threshold;
			envDec.cloneParam(static_cast<DSP *>(&other->envDec));
		}
	}
	std::shared_ptr<DSP> NoiseGate::cloneDSP(){
		return std::make_shared<NoiseGate>(threshold, envDec.attack, envDec.hold, envDec.release, envDec.type);
	}
	String NoiseGate::toString()const{
		return StringFormat::object2string("NoiseGate", threshold, envDec.attack, envDec.hold, envDec.release, envDec.type);
	}
}